<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>系统设置</el-breadcrumb-item>
      <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
      <el-breadcrumb-item>菜单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="table-wrap-box">
      <div class="header-box">
        <el-input style="width: 400px" size="medium" placeholder="请输入搜索内容" v-model="searchParams">
          <el-button size="mini" slot="append" icon="el-icon-search" @click="search"></el-button>
        </el-input>
        <div class="top-btns-box">
          <el-button size="mini" style="margin-left: 40px" type="primary" @click="addMenuClick">添加菜单</el-button>
          <el-button size="mini" style="margin-left: 10px" type="danger" @click="batchDelete" >批量删除</el-button>
          <el-button size="mini" style="margin-left: 10px" >设置布局</el-button>
        </div>
      </div>
      <div class="menu-table">
        <el-table
            size="mini"
            :data="menuData"
            border
            style="width: 100%"
            row-key="id"
            :tree-props="{children: 'children'}"
            @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="40">
          </el-table-column>
          <el-table-column align="center" v-for="item in columns" :prop="item.prop" :label="item.label" :width="item.width">
            <template slot-scope="scope">
              <span v-if="item.oper">
                <el-button size="mini" type="text">编辑</el-button>
                <el-button size="mini" type="text">查看详情</el-button>
                <el-button size="mini" type="text">删除</el-button>
              </span>
              <span v-else>
                <span v-if="item.prop === 'location'">
                  <span v-if="scope.row[item.prop] === 'TOP'">顶部</span>
                  <span v-else>侧栏</span>
                </span>
                <span v-else-if="item.prop === 'type'">
                  <span v-if="scope.row[item.prop] === 'SYSTEM'">系统</span>
                  <span v-else>用户</span>
                </span>
                <span v-else>{{scope.row[item.prop]}}</span>
              </span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>

    <!-- 新增菜单弹出框 start   -->
    <MenuFormDlg
        :show="showMenuDlg"
        :title="menuDlgTitle"
        @dlg-close="handleDlgClose"
        @ok-click="handleOkClick">
      <template #content>
        <MenuForm ref="menuForm" />
      </template>
    </MenuFormDlg>
    <!-- 新增菜单弹出框 end   -->

  </div>
</template>

<script>
import {$SysMenuApi} from "@/network/menu/SysMenuApi";
import MenuFormDlg from "@/components/menus/MenuFormDlg";
import MenuForm from "@/components/menus/MenuForm";
import {CommUtil} from "@/utils/CommonUtil";
export default {
  name: "MenuList",
  components: {MenuForm, MenuFormDlg},
  data() {
    return {
      columns: [
        {prop: 'name', label: '名称', width: "160", oper: false },
        {prop: 'text', label: '显示名称', width: "100", oper: false},
        {prop: 'orderNum', label: '显示顺序', width: "70", oper: false},
        {prop: 'location', label: '位置', width: "60", oper: false},
        {prop: 'type', label: '业务类型', width: "80", oper: false},
        {prop: 'description', label: '描述', width: "200", oper: false},
        {prop: 'defaultActive', label: '是否默认激活', width: "100", oper: false},
        {prop: 'lastModifyTime', label: '更新时间', width: "150", oper: false},
        {prop: 'lastModifyUser', label: '更新人', width: "100", oper: false},
        {label: "操作", width: "160", oper: true}
      ],
      menuData: [],
      searchParams: "",
      showMenuDlg: false,
      menuDlgTitle: '',
      checkedIds: [],
    }
  },

  methods: {
    async loadMenuData() {
      let searchParams = this.searchParams;
      let res = await $SysMenuApi.getMenuVoList(searchParams);
      this.menuData = res.data;
    },
    search() {
      this.loadMenuData();
    },

    addMenuClick() {
      this.showMenuDlg = true;
    },

    handleDlgClose() {
      this.showMenuDlg = false;
    },

    async handleOkClick() {
      const formParams = this.$refs.menuForm.takeFormData();
      formParams['defaultActive'] = formParams['defaultActive'] ? 1 : 0;
      if (formParams['topId'] === "" && formParams["location"] === "top") {
        formParams['parentId'] = "-1";
      } else if (formParams['topId'] !== "" && formParams['sideId'] === "") {
        formParams['parentId'] = formParams['topId'];
      } else {
        formParams['parentId'] = formParams['sideId'];
      }
      delete formParams.topId;
      delete formParams.sideId;
      let res = await $SysMenuApi.addMenu(formParams);
      if (this.$ResHandle.isOk(res)) {
        this.$message.success("菜单添加成功!");
        this.showMenuDlg = false;
        await this.loadMenuData();
      } else {
        this.$message.error(res.msg);
      }
    },

    handleSelectionChange(val) {
      this.checkedIds = val.map(item => {
        return item.id;
      })
    },

    batchDelete() {
      let ids = CommUtil.toPlainObject(this.checkedIds);
      if (!ids || ids.length === 0) {
        this.$message.error("请选择要删除的菜单");
        return;
      }
      this.$confirm('将删除选择的菜单项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await $SysMenuApi.batchDelete({ids});
        if (this.$ResHandle.isOk(res)) {
          this.$message.success("菜单删除成功!");
          await this.loadMenuData();
        }
      }).catch(err => {
        this.$message.error(`菜单删除失败:  ${err}`);
      })
    }

  },

  created() {
    this.loadMenuData();
  },

  mounted() {

  }


}
</script>

<style scoped>
  .table-wrap-box {
    margin-top: 20px;
    height: 100%;
  }
  .header-box {
    width: 100%;
    display: flex;
    justify-content: space-between;
    position: relative;
  }
  .menu-table {
    margin-top: 10px;
  }
  .top-btns-box {
    display: flex;
    position: absolute;
    right: 10px;
  }
</style>